package top.myhdg.bijou.sqlite;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import java.util.ArrayList;
import java.util.List;

public class HistoryDao {
    private final String[] HISTORY_COLUMNS = new String[]{"Time", "Title", "Website"};

    private HistoryDBHelper dbHelper;

    public HistoryDao(Context mContext) {
        dbHelper = new HistoryDBHelper(mContext);
    }

    //=====判断表中是否有数据=====
    public boolean isDataExist() {
        int count = 0;

        SQLiteDatabase db = null;
        Cursor cursor = null;

        try {
            db = dbHelper.getReadableDatabase();
            cursor = db.query(HistoryDBHelper.TABLE_NAME, new String[]{"COUNT(Time)"}, null, null, null, null, null);

            if (cursor.moveToFirst()) {
                count = cursor.getInt(0);
            }
            if (count > 0) return true;
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (cursor != null) {
                cursor.close();
            }
            if (db != null) {
                db.close();
            }
        }
        return false;
    }

    //=====获取所有数据=====
    public List<History> getAllDate() {
        SQLiteDatabase db = null;
        Cursor cursor = null;

        try {
            db = dbHelper.getReadableDatabase();
            cursor = db.query(HistoryDBHelper.TABLE_NAME, HISTORY_COLUMNS, null, null, null, null, null);

            if (cursor.getCount() > 0) {
                List<History> historyList = new ArrayList<>(cursor.getCount());
                while (cursor.moveToNext()) {
                    historyList.add(parseHistory(cursor));
                }
                return historyList;
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (cursor != null) {
                cursor.close();
            }
            if (db != null) {
                db.close();
            }
        }

        return null;
    }

    //=====将数据转换为History类=====
    private History parseHistory(Cursor cursor) {
        History History = new History();
        History.setTitle(cursor.getString(cursor.getColumnIndex("Title")));
        History.setWebsite(cursor.getString(cursor.getColumnIndex("Website")));
        History.setTime(cursor.getString(cursor.getColumnIndex("Time")));
        return History;
    }

    //=====插入一条数据=====
    public void insertDate(History History) {
        SQLiteDatabase db = null;

        String title = History.getTitle();
        String website = History.getWebsite();
        String time = History.getTime();
        try {
            db = dbHelper.getWritableDatabase();
            db.beginTransaction();

            ContentValues contentValues = new ContentValues();
            contentValues.put("Time", time);
            contentValues.put("Title", title);
            contentValues.put("Website", website);
//            db.insertOrThrow(HistoryDBHelper.TABLE_NAME, null, contentValues);
            db.replaceOrThrow(HistoryDBHelper.TABLE_NAME, null, contentValues);
            db.setTransactionSuccessful();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (db != null) {
                db.endTransaction();
                db.close();
            }
        }
    }

    //=====删除一条数据=====
    public void deleteHistory(History History) {
        SQLiteDatabase db = null;

        String time = History.getTime();
        try {
            db = dbHelper.getWritableDatabase();
            db.beginTransaction();
            db.delete(HistoryDBHelper.TABLE_NAME, "Time = ?", new String[]{time});
            db.setTransactionSuccessful();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (db != null) {
                db.endTransaction();
                db.close();
            }
        }
    }
}
